<?php
class App_City
{
	private static $_instance = NULL;
	public static  function getCityListCn()
	{
		$query=DB::select()->from('mod_city')
		       ->execute()->as_array();
	 	self::instance()->getRebuildData($query,0);
		$citys=array();
		foreach($query as $row)
		{
			$id=$row['id'];
		    $country=$row['countryName'];
		    $city=$row['cityName'];
		 	$citys[$country][$id]=$city;
		}
		return $citys;
	}
	public static function getCityListCnWithId()
    {
        $query=DB::select()->from('mod_city')
               ->execute()->as_array();
        self::instance()->getRebuildData($query,0);
        $citys=array();
        foreach($query as $row)
        {
           $id=$row['id'];
           $city=$row['cityName'];
           $citys[$id]=$city;
        }
        return $citys;
    }
    public static function instance()
	{
		if (empty(self::$_instance))
			self::$_instance = new App_City;
	
		return self::$_instance;
	}
    public static function getNameCnById($cityId,$column)
    {
        if(empty($cityId))
        {return null;}
        if(is_array($cityId))
        {
           $ret=array();
           $query=DB::select()->from('mod_city')
           ->where('id','in',$cityId)
           ->execute()->as_array();
           self::instance()->getRebuildData($query, 0);
           foreach($query as $row)
           {
             $ret[$row['id']]=$row[$column];
           }
           return $ret;
        }
        else
        {
           $query=DB::select()->from('mod_city')
           ->where('id','=',$cityId)
           ->limit(1)
           ->execute()->as_array();
           self::instance()->getRebuildData($query, 0);
           return $query[0][$column];
        }
    }
    
    //0:中文,1:英文
	public function getRebuildData(&$query,$language)
	{
		foreach($query as &$row)
	    {
		  foreach ($row as &$v)
		  {
			if(strpos($v, '|')!==FALSE)
			{
			  $v = explode('|',$v);
             		  $v=trim($v[$language]);   
			}
					       
		   }
		}
		
	}
}
